/*******************************************************************************
 * 版权：北京润尼尔网络科技有限公司，保留所有版权<br>
 * 版本：v1.0.0<br>
 * 文件：DeviceMenu.js<br>
 * 日期：2016年10月27日<br>
 * 作者: 杨顺强<br>
 * 功能： 右键界面上图元时，显示对应的菜单<br>
 * 
 * 修改记录：<br>
 * 日期 描述 更新功能<br>
 ******************************************************************************/

window.makeNamespace(window,"Rainier.UI.V2");


/**
 * 类型名称：Rainier.UI.V2.DeviceMenu<br>
 * 类型功能：右键菜单对象<br>
 */
Rainier.UI.V2.DeviceMenu = Rainier.UI.V2.BaseMenu.extend({
	
	NAME : "Rainier.UI.V2.DeviceMenu",
	
	/**
	 * 函数名称：Rainier.UI.V2.DeviceMenu.init<br>
	 * 函数功能：完成图元菜单的初始化<br>
	 * 参数输入：canvasEditPart：图元所在canvas的控制器<br>
	 * 参数输入：Figure		：图元对象及对应点击位置<br>
	 * 参数输出：无<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	init : function(canvasEditPart,figure){
		if(canvasEditPart instanceof Rainier.UI.V2.RootEditPart){
			this.canvasEditPart = canvasEditPart;
		}else{
			throw new Error("传入的canvasEditPart有误，请检测代码，进行修改");
		}
		this.arguments 	= figure;
		this.positionX 	= this.arguments.x;
		this.positionY 	= this.arguments.Y;
		this.figure 	= this.arguments.figure;
		
		this.canvas 	= this.canvasEditPart.getFigure();
		
		this._super(this.canvas,this.canvasEditPart,this.positonX,this.positionY);
		
		//图元对应的editPart是：其外面的矩形（Figure）对应的editPart
		//所以当前图元的EditPart是通过其父亲找到了
		var currentFigure = null;
		if(this.figure.parent){
			currentFigure = this.figure.parent;
		}else{
			currentFigure = this.figure;
		}
		this.figureEditPart = this.canvas.context.findEditPartByFigure(currentFigure);

		var selected  = 0;
		var copyModel = 0;
		selected  = this.canvas.context.getSelected().getSize();
		copyModel = this.canvas.context.copyModelList.getSize();

		var customItems 		= {};
		customItems.copy			= {name:"<span>复制</span><span>Ctrl+C</span>",isHtmlName:true,icon:"copy",};
		customItems.cut			    = {name:"<span>剪切</span><span>Ctrl+X</span>",isHtmlName:true,icon:"cut",};
		customItems.paste			= {name:"<span>粘贴</span><span>Ctrl+V</span>",isHtmlName:true,
										icon:function(opt, $itemElement, itemKey, item){
											if(copyModel > 0){
												return 'context-menu-icon context-menu-icon-paste';
											}
											return 'context-menu-icon context-menu-icon-paste1';
										},
										disabled:function(itemKey,opt){
											if(copyModel > 0){
												return false;
											}
											return true
										}};
		customItems._delete			= {name:"<span>删除</span><span>Delete</span>",isHtmlName:true,icon:"delete",};
		customItems.clearTable		= {name:"<span>清空实验台</span>",isHtmlName:true,icon:"clear-ExprimentTable",disabled:true};
		
		this.items = $.extend(this.items,customItems);
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.DeviceMenu.show<br>
	 * 函数功能：显示创建的菜单<br>
	 * 参数输入：无<br>
	 * 参数输出：无<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	show : function(){
		this._super();
	},
	
	
	/**
	 * 函数名称：Rainier.UI.V2.DeviceMenu.blandBackGround<br>
	 * 函数功能：完成当前对象的复制命令<br>
	 * 参数输入：无<br>
	 * 参数输出：无<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	copy : function(){
		var _this = this;
		var selected = this.canvas.context.getSelected();
    	var editParts = new draw2d.util.ArrayList();
			selected.clone().each(function(j,select){
    		var editPart = _this.canvas.context.findEditPartByModel(select);
    		if(editPart){
    			editParts.add(editPart);
    		}
        })
		var cmd = new Rainier.UI.V2.CommandCopy(this.canvas.context,editParts);
		this.canvas.getCommandStack().execute(cmd);
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.DeviceMenu.blandBackGround<br>
	 * 函数功能：完成当前对象的剪切命令<br>
	 * 参数输入：无<br>
	 * 参数输出：无<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	cut : function(){
		var _this = this;
		var selected = this.canvas.context.getSelected();
    	var editParts = new draw2d.util.ArrayList();
			selected.clone().each(function(j,select){
    		var editPart = _this.canvas.context.findEditPartByModel(select);
    		if(editPart){
    			editParts.add(editPart);
    		}
        })
		var cmd = new Rainier.UI.V2.CommandCut(this.canvas.context,editParts);
		this.canvas.getCommandStack().execute(cmd);
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.DeviceMenu._delete<br>
	 * 函数功能：完成当前对象的删除命令<br>
	 * 参数输入：无<br>
	 * 参数输出：无<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	_delete : function(){
		var _this = this;
		var figures   = this.canvas.getSelection().all;
		this.canvas.getCommandStack().startTransaction(draw2d.Configuration.i18n.command.deleteShape);
		figures.each(function(index, figure){
           var cmd = figure.createCommand(new draw2d.command.CommandType(draw2d.command.CommandType.DELETE));
           if(cmd!==null){
        	   _this.canvas.getCommandStack().execute(cmd);
           }
       });
       // execute all single commands at once.
		this.canvas.getCommandStack().commitTransaction();
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.DeviceMenu.clockWise<br>
	 * 函数功能：实现当前图元的顺时针翻转<br>
	 * 参数输入：无<br>
	 * 参数输出：无<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	clockWise : function(){
		
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.DeviceMenu.reverseClockWise<br>
	 * 函数功能：实现当前图元的逆时针翻转<br>
	 * 参数输入：无<br>
	 * 参数输出：无<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	reverseClockWise : function(){
		
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.DeviceMenu.horizontalFlip<br>
	 * 函数功能：实现当前图元水平翻转<br>
	 * 参数输入：无<br>
	 * 参数输出：无<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	horizontalFlip : function(){
		
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.DeviceMenu.verticesFlip<br>
	 * 函数功能：实现当前图元的垂直翻转<br>
	 * 参数输入：无<br>
	 * 参数输出：无<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	verticesFlip : function(){
		
	},
});
